#include "generated.h"

using namespace std;
using namespace uhdpbs::db;
using namespace uhdpbs::generated;

namespace uhdpbs {
	namespace menu {
		void generated::edit_row(row* r) {
			int iStartPosition = 1;
			while(true) {
				string sTitle = r->get_table()->get_connection()->get_user_freidnly_name(r->get_table()->get_name()) + " Record";
				string_vector* psvColumns = r->get_table()->get_columns();

				Menu mnu = Menu(sTitle, " > ", "");

				for(int cnt = 0; cnt < psvColumns->size(); cnt++) {		
					string sColName = (*psvColumns)[cnt];
					string sNiceName = r->get_table()->get_connection()->get_user_freidnly_name(sColName);
					string sValue = r->get_value(sColName);

					mnu.AddOption(sNiceName + ": " + sValue);
				}

				mnu.AddOption("Reset Record to Original Values");
				mnu.AddOption("Delete Record and Return");
				mnu.AddOption("Return to Previous Menu");

				int iSelection = mnu.Play(iStartPosition);
				VP_ClearScreen();
				if(iSelection <= psvColumns->size()) {
					string sColName = (*psvColumns)[iSelection - 1];
					if(sColName == "id") {
						cout << "The ID will be assigned when the record is saved." << endl;
						cout << "Press enter to continue...";
						cin.get();
					}else {
						string sNiceName = r->get_table()->get_connection()->get_user_freidnly_name(sColName);

						cout << sNiceName << ": ";

						string sNewValue = "";
						char pcBuffer[1024];
						cin.getline(pcBuffer, sizeof(pcBuffer));

						r->set_value(sColName, pcBuffer);
					}
				} else if(iSelection == psvColumns->size() + 1) {
					r->reset();
				} else if(iSelection == psvColumns->size() + 2) {
					r->is_deleted(true);
					return;
				} else if(iSelection == psvColumns->size() + 3) {
					return;
				} else {
					cout << "Invalid selection" << endl;
					cout << "Press enter to continue...";
					cin.get();
				}

				iStartPosition = iSelection;
			}
		}
		void generated::patient_list(patient_table* tbl) {
			int iStartPosition = 1;
			while(true) {
				Menu mnu = Menu("Patient List", " > ", "");
				for(int cnt = 0; cnt < tbl->get_rows()->size(); cnt++) {
					patient_row r = (patient_row) (*tbl->get_rows())[cnt];

					string sDisplay = r.get_id() + " - " + r.get_last_name() + ", " + r.get_first_name() + " (" + r.get_dob() + ")";
					mnu.AddOption(sDisplay);
				}

				mnu.AddOption("Add New Record");
				mnu.AddOption("Return to Previous Menu");

				int iSelection = mnu.Play(iStartPosition);
				VP_ClearScreen();
				if(iSelection <= tbl->get_rows()->size()) {
					row r = (*tbl->get_rows())[iSelection - 1];
					edit_row(&r);
					(*tbl->get_rows())[iSelection - 1] = r;
					tbl->save();
					tbl->load();
				} else if(iSelection == tbl->get_rows()->size() + 1) {
					row r = tbl->create_new_row();
					edit_row(&r);
					tbl->get_rows()->push_back(r);
					tbl->save();
					tbl->load();
				} else if(iSelection == tbl->get_rows()->size() + 2) {
					return;
				} else {
					cout << "Invalid selection" << endl;
					cout << "Press enter to continue...";
					cin.get();
				}

				iStartPosition = iSelection;
			}
		}
	}
}